package lib;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
* Created by IntelliJ IDEA.
* User: an.bogdanov
* Date: 19.10.2010
* Time: 13:52:40
* To change this template use File | Settings | File Templates.
*/
public class DOMNode {
	
	
    private char[] value = new char[0];
    private final Hashtable attributes = new Hashtable(11);
    
    private final Vector attributes_name = new Vector();
    private final Vector attributes_value = new Vector();
    
    
    private final Vector nodes = new Vector(5);
    private final char[] name;

    /**
     * add new node to current node
     * @param value new DOMNode
     * @return new node
     */
    public DOMNode addNode(DOMNode value) { nodes.addElement(value); return value; }

    /**
     * create new node with name "name" and add this to current node
     * @param name for new node
     * @return new node
     */
    public DOMNode addNode(String name) { return addNode( new DOMNode(name) ); }

    /**
     * create new node and set value to new node
     * @param name for new node
     * @param value of new node
     * @return new node
     */
    public DOMNode addNode(String name, Object value) { return addNode(name).setValue(value); }

    /**
     * create new node and set value to new node
     * @param name for new node
     * @param value of new node
     * @return new node
     */
    public DOMNode addNode(String name, long value) { return addNode(name, String.valueOf(value)); }

    /**
     * add attribute to current node
     * @param name for attribute
     * @param value for attribute
     * @return current node
     */
    public DOMNode addAttribute(String name, Object value) { attributes.put(name, value == null ? "" : value); return this; }
    
    public DOMNode addAttribute1(String name, Object value) 
    {
    	//attributes.put(name, value == null ? "" : value);
    	attributes_name.addElement(name);
    	attributes_value.addElement(value);
    	return this; 
    }

    
    /**
     * add attribute to current node
     * @param name for attribute
     * @param value for attribute
     * @return current node
     */
    public DOMNode addAttribute(String name, long value) { return addAttribute(name, String.valueOf(value)); }
    
    public DOMNode addAttribute1(String name, long value) { return addAttribute1(name, String.valueOf(value)); }

    /**
     * create new node
     * @param name for attribute
     */
    public DOMNode(String name) { this.name = name.toCharArray(); }

    /**
     * set value to current node
     * @param value for attribute
     * @return current node
     */
    public DOMNode setValue(Object value) { if (value != null) this.value = value.toString().toCharArray(); return this; }
    
    public DOMNode setValue1(Object value) { if (value != null) this.value = value.toString().toCharArray(); return this; }
    
    /**
     * set value to current node
     * @param value for attribute
     * @return current node
     */
   // public DOMNode setValue(long value) { return setValue(String.valueOf(value)); }

    /**
     * compile string from current node
     * @return string
     */
    
    public String toString1()
    {
    	 StringBuffer sb = new StringBuffer(200);
         sb.append('<').append(name);
         
//         if (attributes.size() > 0) {
//             Enumeration k = attributes.keys();
//             Enumeration v = attributes.elements();
//             while (k.hasMoreElements())
//                 sb.append(' ').append(k.nextElement()).append('=').append('"').append(v.nextElement()).append('"');
//         }
         
         
 	      if (!attributes_name.isEmpty()) {
 	    	  
 	    	 int length = attributes_name.size();
 	    	 
 	    	 for(int i = 0;i<length;i++)
 	    	 {
 	    		 sb.append(' ').append(attributes_name.elementAt(i)).append('=').append('"').append(attributes_value.elementAt(i)).append('"');
 	    	 }
 	    	 
 	      Enumeration k = attributes.keys();
 	      Enumeration v = attributes.elements();
 	      while (k.hasMoreElements())
 	          sb.append(' ').append(k.nextElement()).append('=').append('"').append(v.nextElement()).append('"');
 	      }
         
         if (nodes.size() == 0 && (value == null || value.length == 0))
             return sb.append('/').append('>').toString();
         sb.append('>');
         if (nodes.size() > 0) {
             for (int i = 0; i < nodes.size(); )
                 sb.append( ((DOMNode)nodes.elementAt(i++)).toString1() );
         } else
             sb.append(value);
         return sb.append('<').append('/').append(name).append('>').toString();
    }
    
    public String toString() {
        StringBuffer sb = new StringBuffer(200);
        sb.append('<').append(name);
        
        if (attributes.size() > 0) {
            Enumeration k = attributes.keys();
            Enumeration v = attributes.elements();
            while (k.hasMoreElements())
                sb.append(' ').append(k.nextElement()).append('=').append('"').append(v.nextElement()).append('"');
        }
        
        if (nodes.size() == 0 && (value == null || value.length == 0))
            return sb.append('/').append('>').toString();
        sb.append('>');
        if (nodes.size() > 0) {
            for (int i = 0; i < nodes.size(); )
                sb.append( nodes.elementAt(i++) );
        } else
            sb.append(value);
        return sb.append('<').append('/').append(name).append('>').toString();
    }


}
